// pages/detail/detail.js
//获取应用实例
const app = getApp();
var timer = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,
    data: "",
    comment: { text: '', money: '', },
    time: '',
    user_list: "",
  },
  //开启我的立减金
  Invitation_click: function () {
    wx.redirectTo({
      url: '/pages/reducemoney/index/index?id=' + this.data.id,
    })
  },
  //使用优惠劵
  go_home: function () {
    var that = this;
    console.log('使用优惠劵');
    wx.redirectTo({
      url: that.data.data.url,
    })
  },
  // 商品推荐列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var token_key = wx.getStorageSync('token_key');
    console.log(token_key);
    if (token_key == undefined || token_key == '') {
      app.getUserInfo(function () {
        load_fn(that);
      })
    } else {
      load_fn(that)
    }

    function load_fn(that) {
      //获取好友进入的信息
      wx.request({
        url: app.url + 'api_reduce_money/add_reduce_money',
        data: {
          // id: options.id,
          // sign: options.sign,
          id: 4,
          sign: '2017122161987782',
          type: 2,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          console.log(res);
          var message = '', result_text='';
          if (res.data.result == '成功') {
            prompt('领取成功', 'success');
          } else if (res.data.message == 7) {
            prompt('没有该活动', 'loading');
          } else if (res.data.message == 8) {
            prompt('活动已过期', 'loading');
          } else if (res.data.message == 9) {
            prompt('领取失败', 'loading');
          } else if (res.data.message == 10) {
            prompt('请先完成活动', 'loading');
          } else if (res.data.message == 11) {
            prompt('您来迟啦~', 'loading');
          } else if (res.data.message == 12) {
            prompt('你已经领取了', 'loading');
          } else {
            prompt('领取失败', 'loading');
          }

          //提示信息
          function prompt(text, status) {
            wx.showToast({
              title: text,
              icon: status,
              duration: 1200,
            })
          }

          that.setData({
            message: (res.data.message ? res.data.message : ''),
            result_text: res.data.result,
          })

          //添加好友信息显示
          wx.request({
            url: app.url + 'api_reduce_money/reduce_money_details',
            data: {
              sign: options.sign,
            },
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key')
            }, success: function (res) {
              console.log(res);

              var time = 86400;
              that.setData({
                time_num: time,
                data: res.data.result,
                user_list: (res.data.result.user_list ? res.data.result.user_list : ''),
                id: options.id,
              })
            },
          })

          //猜你喜欢
          wx.request({
            url: app.url + 'api_footprint/footprint_group_list',
            data: {
            },
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key')
            },
            success: function (json) {
              that.setData({
                likegoods: json.data.result,
              })
            }
          })


        },
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var time = that.data.time_num;
    timer = setInterval(function () {
      if (time > 0) {
        var h = Math.floor(time / (60 * 60));
        var m = Math.floor(time / 60) - (h * 60);
        var s = Math.floor(time) - (h * 60 * 60) - (m * 60);

        h = calculation(h);
        m = calculation(m);
        s = calculation(s);
        function calculation(data) {
          if (('' + data).length == 1) {
            return ('0' + data);
          } else {
            return data
          }
        }
        var falling_down = h + '时' + m + '分' + s + '秒';
        // console.log(falling_down);
        --time;
        that.setData({
          time_num: time,
          time: falling_down,
        })
      } else {
        clearInterval(timer);
        timer = '';
      }
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var share_img = that.data.data.share_img;
    var imageUrl = that.data.imgurl + share_img.substring(1);
    console.log(that.data.data);
    // var imageUrl = "http://www.ltwx.com/" + share_img.substring(1);// 测试 要删掉
    console.log(imageUrl);
    console.log('/pages/index/detail/open_red_bag/index?id=' + that.data.data.id + '&sign=' + that.data.data.sign);
    return {
      imageUrl: imageUrl,
      title: that.data.data.title, // 标题
      path: '/pages/reducemoney/detail/index?id=' + that.data.data.id + '&sign=' + that.data.data.sign, // 分享后点击页面链接
      success: function (res) {

      }
    }
  }
})